package com.zxj.geekstudy.service.impl;

import com.google.common.collect.Lists;
import com.zxj.geekstudy.mapper.UserOrderLogMapper;
import com.zxj.geekstudy.model.UserOrderLog;
import com.zxj.geekstudy.service.UserOrderLogService;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

@Service
public class UserOrderLogServiceImpl implements UserOrderLogService {

    @Resource
    private UserOrderLogMapper userOrderLogMapper;

    @Override
    @Transactional
    @ShardingTransactionType(TransactionType.XA)
    public void addUserOrderLogsXATransaction(List<UserOrderLog> userOrderLogs) throws SQLException {
        for (int i = 0; i < userOrderLogs.size(); i++) {
            userOrderLogMapper.addUserOrderLogs(Lists.newArrayList(userOrderLogs.get(i)));
//            if (i == 8) {
//                throw new RuntimeException("模拟保存失败！！！！！！！！！！！！！！！");
//            }
        }
    }

    @Override
    public void addUserOrderLogs(List<UserOrderLog> userOrderLogs) {
        userOrderLogMapper.addUserOrderLogs(userOrderLogs);
    }

    @Override
    public List<UserOrderLog> list(UserOrderLog userOrderLog) {
        return userOrderLogMapper.list(userOrderLog);
    }

}
